package com.example.demo.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * createby : lbj
 * date : 2018/7/10
 * desc :
 **/

public class SimpleChatServerInitializer extends ChannelInitializer<SocketChannel> {
//
//    // 读超时
//    private static final int READ_IDEL_TIME_OUT = 300;
//    // 写超时
//    private static final int WRITE_IDEL_TIME_OUT = 300;
//    // 所有超时
//    private static final int ALL_IDEL_TIME_OUT = 300;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        //心跳
//        pipeline.addLast(new IdleStateHandler(READ_IDEL_TIME_OUT,
//                WRITE_IDEL_TIME_OUT, ALL_IDEL_TIME_OUT, TimeUnit.SECONDS));
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast("handler", new SimpleChatServerHandler());
        System.out.println("SimpleChatClient:" + socketChannel.remoteAddress() + "连接上");
    }
}
